package models

import (
	"zhanyan/pac/util"
)

// Classroom 教室
type Classroom struct {
	ID        int    `json:"id";gorm:"primary_key"`
	Name      string `json:"name"`
	Location  string `json:"location"`
	Type      string `json:"type"`
	TeacherID int
	Seat      []Seat
}

// CheckClassroom 根据教室名查询教室
func CheckClassroom(name string) (*Classroom, error) {
	classroom := Classroom{Name: name}
	err := db.Where(classroom).First(&classroom).Error
	if err != nil {
		return nil, util.ClassroomNotExistError
	}
	if classroom.ID > 0 {
		return &classroom, nil
	}
	return nil, util.ClassroomNotExistError
}

// CheckClassroomByID 根据ID查询教室
func CheckClassroomByID(classroomID int) (bool, error) {
	classroom := Classroom{ID: classroomID}
	err := db.First(&classroom).Error
	if err != nil {
		return false, util.ClassroomNotExistError
	}
	if classroom.ID > 0 {
		return true, nil
	}
	return false, util.ClassroomNotExistError
}

// AddClassroom 添加教室
func AddClassroom(classname *Classroom) (id int, err error) {
	err = db.Create(classname).Error
	if err != nil {
		return 0, util.ClassroomNotAddedError
	}
	return classname.ID, nil
}

// UpdatedClassroom 更新教室
func UpdatedClassroom(classname *Classroom) error {
	err := db.Save(classname).Error
	if err != nil {
		return util.ClassroomUpdateError
	}
	return nil
}

// DeleteClassroom  删除教室
func DeleteClassroom(classroomID int) error {
	err := db.Delete(&Classroom{ID: classroomID}).Error
	if err != nil {
		return util.ClassroomDeleteError
	}
	return nil
}

// DeleteClassroomInSeats 删除教室中所有座位的关联
func DeleteClassroomInSeats(classroomID int) error {
	class := Classroom{ID: classroomID}
	err := db.Model(&class).Association("Seat").Clear().Error
	if err != nil {
		return util.ClassroomDeleteSeatError
	}
	return nil
}

// QueryClassroom 查询教室
func QueryClassroom(id int) (*Classroom, error) {
	classroom := Classroom{ID: id}
	err := db.Find(&classroom).Error
	if err != nil {
		return nil, util.ClassroomQueryError
	}
	return &classroom, nil
}

// QueryClassroomAndSeats 查询教室带着座位
func QueryClassroomAndSeats(id int) (*Classroom, error) {
	classroom, err := QueryClassroom(id)
	if err != nil {
		return nil, err
	}
	seats := []Seat{}
	err = db.Model(classroom).Association("Seat").Find(&seats).Error
	if err != nil {
		return nil, err
	}
	classroom.Seat = seats
	return classroom, nil
}

// QueryClassroomByTid 根据教师id查询教室
func QueryClassroomByTid(tid int) (*[]Classroom, error) {
	teacher := Teacher{User: User{ID: tid}}
	classrooms := []Classroom{}
	err := db.Model(&teacher).Association("Classroom").Find(&classrooms).Error
	if err != nil {
		return nil, util.ClassroomQueryError
	}
	return &classrooms, nil
}

// QueryClassroomPageByTid 根据教师id分页查询教室
func QueryClassroomPageByTid(tid, ps, pc int) (*[]Classroom, error) {
	classrooms := []Classroom{}
	err := db.Where("teacher_id = ?", tid).Limit(ps).Offset(pc * ps).Find(&classrooms).Error
	if err != nil {
		return nil, util.ClassroomQueryError
	}
	return &classrooms, nil
}

// QueryClassroomByTidNumber 根据教师id查询教室数量
func QueryClassroomByTidNumber(tid int) (int64, error) {
	classrooms := []Classroom{}
	find := db.Where("teacher_id = ?", tid).Find(&classrooms)
	if find.Error != nil {
		return 0, util.ClassroomQueryError
	}
	return find.RowsAffected, nil
}

// QueryAllClassroom 查询所有教室
func QueryAllClassroom() (*[]Classroom, error) {
	//teacher := Teacher{}
	classrooms := []Classroom{}
	err := db.Find(&classrooms).Error
	if err != nil {
		return nil, util.ClassroomQueryError
	}
	return &classrooms, nil
}

// QueryAllClassroomNumber 查询所有教室总数
func QueryAllClassroomNumber() (int64, error) {
	classrooms := []Classroom{}
	find := db.Find(&classrooms)
	if find.Error != nil {
		return 0, util.ClassroomQueryError
	}
	return find.RowsAffected, nil
}

// QueryClassroomPage 分页查找教室
func QueryClassroomPage(pageSize int, pageCode int) (*[]Classroom, error) {
	classrooms := &[]Classroom{}
	err := db.Limit(pageSize).Offset(pageCode * pageSize).Find(&classrooms).Error
	if err != nil {
		return nil, util.ClassroomQueryError
	}
	return classrooms, nil
}

// QueryAllClassroomByName 根据教室名查询所有教室
func QueryAllClassroomByName(name string) (*[]Classroom, error) {
	classrooms := []Classroom{}
	err := db.Where("name LIKE ?", "%"+name+"%").Find(&classrooms).Error
	if err != nil {
		return nil, util.ClassroomQueryError
	}
	return &classrooms, nil
}

// QueryClassroomByNameAndTid 根据教室名和教师id查询所有教室
func QueryClassroomByNameAndTid(name string, tid int) (*[]Classroom, error) {
	classrooms := []Classroom{}
	err := db.Where("name LIKE ?", "%"+name+"%").
		Where("teacher_id = ?", tid).Find(&classrooms).Error
	if err != nil {
		return nil, util.ClassroomQueryError
	}
	return &classrooms, nil
}

// QueryClassroomPageByNameAndTid 根据教室名和教师id分页查询所有教室
func QueryClassroomPageByNameAndTid(name string, tid, ps, pc int) (*[]Classroom, error) {
	classrooms := []Classroom{}
	err := db.Where("name LIKE ?", "%"+name+"%").
		Where("teacher_id = ?", tid).
		Limit(ps).Offset(pc * ps).
		Find(&classrooms).Error
	if err != nil {
		return nil, util.ClassroomQueryError
	}
	return &classrooms, nil
}

// QueryClassroomByNameAndTidNum 根据教室名和教师id查询所有教室数量
func QueryClassroomByNameAndTidNum(name string, tid int) (int64, error) {
	classrooms := []Classroom{}
	find := db.Where("name LIKE ?", "%"+name+"%").Where("teacher_id = ?", tid).Find(&classrooms)
	if find.Error != nil {
		return 0, util.ClassroomQueryError
	}
	return find.RowsAffected, nil
}

// QueryAllClassroomByNameNumber 根据教室名查询所有教室数量
func QueryAllClassroomByNameNumber(name string) (int64, error) {
	classrooms := []Classroom{}
	find := db.Where("name LIKE ?", "%"+name+"%").Find(&classrooms)
	if find.Error != nil {
		return 0, util.ClassroomQueryError
	}
	return find.RowsAffected, nil
}

// QueryClassroomPageByName 根据教师名分页查找教室
func QueryClassroomPageByName(name string, pageSize int, pageCode int) (*[]Classroom, error) {
	classrooms := &[]Classroom{}
	err := db.Where("name LIKE ?", "%"+name+"%").Limit(pageSize).Offset(pageCode * pageSize).Find(&classrooms).Error
	if err != nil {
		return nil, util.ClassroomQueryError
	}
	return classrooms, nil
}
